%calibration of cross-sectional moments. This code generates data that is
%then  used for regression in stata to construct Table 5 of Appendix B in the paper

clear
delta = .01; %housing depreciation
alpha = [.14, .21] ; %fraction of non-borrowers in high and low states
pl_share = [.25,.07]; %private-label share in high and low states
alpha_h = [.332,.25]; %fraction of high-income borrowers in high and low states
phi_bh = [.98,.95]; %high-income borrower endowment receival probability in high and low state
phi_bl = [.88,0.75]; %low-income borrower endowment receival probability in high and low state
e = .95; %borrower endowment
c = 1; %house price supply parameter
N=2; %number of large "banks"
q_s = [.9,.1;.01,.99]; %state transition matrix
beta =.94;
%%
K=1; %propping-up active


count=1;

%vary PL share to match the MSA-level distribution in oustanding GSE share
%at start of 2007
for i=[.5,.4,.4,.3,.3,.3,.3,.2,.2,.2,.2,.2,.2,.2, .2,.2,.2,.2,.2,.1,.1,.1,.1,.1,.1,.1,.1,.1,.1,.1,.1,.1,.1,.1,.1,.1,.1,.1,.1,.1,.1,.1,.1,0,0,0,0,0,0,0,0,0,0,0,0]

    pl_share(1) = i+.01;
    diff = .25-.07;
    pl_share(2) = pl_share(1)-diff;
    [g_temp,index_temp,p_temp] = vfi_benchmark(K,N,alpha,pl_share, alpha_h, phi_bh, phi_bl, q_s, beta,delta,c,e);

    %Choose initial value of loans
    k=50;
    %2006H1 -- first high shock 
    m(1,1) = N*g_temp(k(1,1),1);
    m_l(1,1) = max(m(1,1)-alpha_h(1),0);
    m_h(1,1)=min(m(1,1),alpha_h(1));

    %2006H2 -- second high shock
    m(2,1)= N*g_temp(k(1,1),1);
    p(2,1) = p_temp(k(1,1),1);
    k(2,1)= index_temp(k(1,1),1);
    m_l(2,1) = max(m(2,1)-alpha_h(1),0);
    m_h(2,1)=min(m(2,1),alpha_h(1));

   %2007H1 - first low shock
   m(3,1)= N*g_temp(k(2,1),2);
   p(3,1) = p_temp(k(2,1),2);
   k(3,1)= index_temp(k(2,1),2);
   m_l(3,1) = max(m(3,1)-alpha_h(2),0);
   m_h(3,1)=min(m(3,1),alpha_h(2));
   
   %2007H2 - second low shock
   m(4,1)= N*g_temp(k(3,1),2);
   p(4,1) = p_temp(k(3,1),2);
   k(4,1)= index_temp(k(3,1),2);
   m_l(4,1) = max(m(4,1)-alpha_h(2),0);
   m_h(4,1)=min(m(4,1),alpha_h(2));
   
   %2008H1 - third low shock
   m(5,1)= N*g_temp(k(4,1),2);
   p(5,1) = p_temp(k(4,1),2);
   k(5,1)= index_temp(k(4,1),2);
   m_l(5,1) = max(m(5,1)-alpha_h(2),0);
   m_h(5,1)=min(m(5,1),alpha_h(2));
  
      
   %2008H2 - fourth low shock
   m(6,1)= N*g_temp(k(5,1),2);
   p(6,1) = p_temp(k(5,1),2);
   m_l(6,1) = max(m(6,1)-alpha_h(2),0);
   m_h(6,1)=min(m(6,1),alpha_h(2));
   
   %model-implied parameters
   
   %growth in low-income loans 2006H2 to 2007H1 and 2007H1 to 2007H2
   li_gr_6h2_7h1(count) = (m_l(3,1)-m_l(2,1))/(m_l(2,1));
   li_fraction_06h1=m_l(1,1)/m(1,1);
   li_fraction_06h2=m_l(2,1)/m(2,1);
   li_fraction_07h1=m_l(3,1)/m(3,1);
   li_fraction_07h2=m_l(4,1)/m(4,1);
   li_fraction_08h1=m_l(5,1)/m(5,1);
   li_fraction_08h2=m_l(6,1)/m(6,1);
   li_fraction_change_06h1_06h2(count) = li_fraction_06h2-li_fraction_06h1;
   li_fraction_change_06h2_07h1(count) = li_fraction_07h1-li_fraction_06h2;
   li_fraction_change_07h1_07h2(count) = li_fraction_07h2-li_fraction_07h1;
   li_fraction_change_07h2_08h1(count) = li_fraction_08h1-li_fraction_07h2;
   li_fraction_change_08h1_08h2(count) = li_fraction_08h2-li_fraction_08h1;
   gse_share_06h2(count) = m(1,1)/(m(1,1)+pl_share(1)+alpha(1));
   gse_share_07h1(count) = m(2,1)/(m(2,1)+pl_share(1)+alpha(1));
   gse_share_07h2(count) = m(3,1)/(m(3,1)+pl_share(2)+alpha(2));
   gse_share_08h1(count) = m(4,1)/(m(4,1)+pl_share(2)+alpha(2));
   m_06h2(count)=m(2,1);
   m_07h1(count)=m(3,1);
   m_07h2(count)=m(4,1);
   m_08h1(count)=m(5,1);
   time_06h2(count)=1;
   time_07h1(count)=2;
   time_07h2(count)=3;
   time_08h1(count)=4;
   time_06h1(count)=0;
   count=count+1;

 end
 


time = [time_06h1 time_06h2 time_07h1 time_07h2 time_08h1];
m_time = [m_06h2 m_06h2 m_07h1 m_07h2 m_08h1];
gse_share_time = [gse_share_06h2 gse_share_06h2 gse_share_07h1 gse_share_07h2 gse_share_08h1];
li_fraction_time = [li_fraction_change_06h1_06h2 li_fraction_change_06h1_06h2 li_fraction_change_06h2_07h1 li_fraction_change_07h1_07h2 li_fraction_change_07h2_08h1];

data_input=[time' gse_share_time' li_fraction_time' m_time'];
dta_input=num2cell(data_input);

cell2csv('sd_stata.csv', dta_input)

